<?php
  // Adapted for The Art of Web: www.the-art-of-web.com
  // Based on PHP code from: php.webmaster-kit.com
  // Please acknowledge use of this code by including this header.

  // initialise image with dimensions of 120 x 30 pixels
  $image = @imagecreatetruecolor(120, 30) or die("Cannot Initialize new GD image stream");

  // set background to white and allocate drawing colours
  $background = imagecolorallocate($image, 251, 252, 234);
  imagefill($image, 0, 0, $background);
  $linecolor = imagecolorallocate($image, 0xCC, 0xCC, 0xCC);
  $textcolor_arr = imagecolorallocate($image, 0x33, 0x33, 0x33);

  // draw random lines on canvas
  for($i=0; $i < 3; $i++) {
    imagesetthickness($image, rand(1,3));
    imageline($image, 0, rand(0,30), 120, rand(0,30), $linecolor);
  }

  //session_start();
	if(!isset($_SESSION)): session_start(); endif;

  $captcha_text = '';
  for($x = 15; $x <= 115; $x += 15) {
    $captcha_text .= ($num = rand(0, 9));
    $new_x = $x - intval(rand(1,10));

    imagechar($image, rand(3, 5), $new_x, rand(2, 14), $num, $textcolor_arr);
  }

  // record digits in session variable
  $_SESSION['sess_captcha'] = $captcha_text;

  // display image and clean up
  header('Content-type: image/png');
  imagepng($image);
  imagedestroy($image);
?>